package com.android.tony;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {
    private final String TAG = "lbingt";

    private ListView mListView;
    private MainAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    private void initView() {
        mListView = getListView();
        ArrayList<String> items = new ArrayList<>();
        items.add("Monday");
        items.add("Tuesday");
        items.add("Wednesday");
        items.add("Thursday");
        items.add("Friday");
        items.add("Saturday");
        items.add("Sunday");
        mAdapter = new MainAdapter(this, items);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(mItemClickListener);
    }

    private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String day = (String) mAdapter.getItem(position);
            Toast.makeText(MainActivity.this, "Today is " + day, Toast.LENGTH_LONG).show();
        }
    };

    private class MainAdapter extends BaseAdapter {
        private Context mContext;
        private ArrayList<String> mItems;

        public MainAdapter(Context context, ArrayList<String> list) {
            mContext = context;
            mItems = list;
        }

        @Override
        public int getCount() {
            return mItems.size();
        }

        @Override
        public Object getItem(int position) {
            return mItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_1, null);

            TextView name = (TextView) convertView.findViewById(android.R.id.text1);
            name.setText(mItems.get(position));

            return convertView;
        }
    }

}
